/*
 * Copyright [2022] [https://www.xiaonuo.vip]
 *
 * Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：
 *
 * 1.请不要删除和修改根目录下的LICENSE文件。
 * 2.请不要删除和修改Snowy源码头部的版权声明。
 * 3.本项目代码可免费商业使用，商业使用请保留源码和相关描述文件的项目出处，作者声明等。
 * 4.分发源码时候，请注明软件出处 https://www.xiaonuo.vip
 * 5.不可二次分发开源参与同类竞品，如有想法可联系团队xiaonuobase@qq.com商议合作。
 * 6.若您的项目无法满足以上几点，需要更多功能代码，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.biz.modular.myexamhis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollStreamUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.solon.service.impl.ServiceImpl;
import org.noear.solon.annotation.Component;
import org.noear.solon.data.annotation.Transaction;
import vip.xiaonuo.common.enums.CommonSortOrderEnum;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.page.CommonPageRequest;
import vip.xiaonuo.biz.modular.myexamhis.entity.ExmMyExamHis;
import vip.xiaonuo.biz.modular.myexamhis.mapper.ExmMyExamHisMapper;
import vip.xiaonuo.biz.modular.myexamhis.param.ExmMyExamHisAddParam;
import vip.xiaonuo.biz.modular.myexamhis.param.ExmMyExamHisEditParam;
import vip.xiaonuo.biz.modular.myexamhis.param.ExmMyExamHisIdParam;
import vip.xiaonuo.biz.modular.myexamhis.param.ExmMyExamHisPageParam;
import vip.xiaonuo.biz.modular.myexamhis.service.ExmMyExamHisService;

import java.util.List;

/**
 * 我的考试历史Service接口实现类
 *
 * @author lrw
 * @date  2025/10/04 09:42
 **/
@Component
public class ExmMyExamHisServiceImpl extends ServiceImpl<ExmMyExamHisMapper, ExmMyExamHis> implements ExmMyExamHisService {

    @Override
    public Page<ExmMyExamHis> page(ExmMyExamHisPageParam exmMyExamHisPageParam) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if(ObjectUtil.isNotEmpty(exmMyExamHisPageParam.getExamId())) {
            queryWrapper.like(ExmMyExamHis::getExamId, exmMyExamHisPageParam.getExamId());
        }
        if(ObjectUtil.isNotEmpty(exmMyExamHisPageParam.getUserId())) {
            queryWrapper.like(ExmMyExamHis::getUserId, exmMyExamHisPageParam.getUserId());
        }
        if(ObjectUtil.isAllNotEmpty(exmMyExamHisPageParam.getSortField(), exmMyExamHisPageParam.getSortOrder())) {
            CommonSortOrderEnum.validate(exmMyExamHisPageParam.getSortOrder());
            queryWrapper.orderBy(StrUtil.toUnderlineCase(exmMyExamHisPageParam.getSortField()),exmMyExamHisPageParam.getSortOrder().equals(CommonSortOrderEnum.ASC.getValue()));
        } else {
            queryWrapper.orderBy(ExmMyExamHis::getSortCode);
        }
        return this.page(CommonPageRequest.defaultPage(), queryWrapper);
    }

    @Transaction
    @Override
    public void add(ExmMyExamHisAddParam exmMyExamHisAddParam) {
        ExmMyExamHis exmMyExamHis = BeanUtil.toBean(exmMyExamHisAddParam, ExmMyExamHis.class);
        this.save(exmMyExamHis);
    }

    @Transaction
    @Override
    public void edit(ExmMyExamHisEditParam exmMyExamHisEditParam) {
        ExmMyExamHis exmMyExamHis = this.queryEntity(exmMyExamHisEditParam.getId());
        BeanUtil.copyProperties(exmMyExamHisEditParam, exmMyExamHis);
        this.updateById(exmMyExamHis);
    }

    @Transaction
    @Override
    public void delete(List<ExmMyExamHisIdParam> exmMyExamHisIdParamList) {
        // 执行删除
        this.removeByIds(CollStreamUtil.toList(exmMyExamHisIdParamList, ExmMyExamHisIdParam::getId));
    }

    @Override
    public ExmMyExamHis detail(ExmMyExamHisIdParam exmMyExamHisIdParam) {
        return this.queryEntity(exmMyExamHisIdParam.getId());
    }

    @Override
    public ExmMyExamHis queryEntity(String id) {
        ExmMyExamHis exmMyExamHis = this.getById(id);
        if(ObjectUtil.isEmpty(exmMyExamHis)) {
            throw new CommonException("我的考试历史不存在，id值为：{}", id);
        }
        return exmMyExamHis;
    }
}
